/**
 * 
 */
package name.webdizz.gadget.four.envelope.client.presenter;

import name.webdizz.gadget.four.envelope.client.EnvelopePreferences;
import name.webdizz.gadget.four.envelope.client.domain.User;
import name.webdizz.gadget.four.envelope.client.domain.EnvelopeSize;
import name.webdizz.gadget.four.envelope.client.event.Events;
import name.webdizz.gadget.four.envelope.client.util.EnvelopeProvider;
import name.webdizz.gadget.four.envelope.client.view.IHomeView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.SetPrefsFeature;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mvp4g.client.presenter.Presenter;

/**
 * This is {@link Presenter} instance for {@link IHomeView} view.
 * 
 * @author Izzet_Mustafayev
 * 
 */
public class HomeViewPresenter extends Presenter<IHomeView> {

	/**
	 * Reference to {@link SetPrefsFeature}.
	 */
	private SetPrefsFeature preferenceFeature;

	/**
	 * Reference to {@link EnvelopePreferences} instance.
	 */
	private EnvelopePreferences envelopePreferences;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvp4g.client.presenter.Presenter#bind()
	 */
	@Override
	public void bind() {
		// register reset credentials button click handler
		view.addResetCredentialsHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				preferenceFeature.set(envelopePreferences.username(), "");
				preferenceFeature.set(envelopePreferences.password(), "");
				eventBus.dispatch(Events.CREDENTIALS_RESETED);
			}
		});
	}

	/**
	 * @param preferenceFeature
	 *            the preferenceFeature to set
	 */
	public void setPreferenceFeature(final SetPrefsFeature preferenceFeature) {
		this.preferenceFeature = preferenceFeature;
	}

	/**
	 * @param envelopePreferences
	 *            the envelopePreferences to set
	 */
	public void setEnvelopePreferences(
			final EnvelopePreferences envelopePreferences) {
		this.envelopePreferences = envelopePreferences;
	}

	/**
	 * Application start point. Here we need to check credentials availability
	 * and perform related to this actions.
	 */
	public void onStart() {
		if (!isCredentialsValid()) {
			eventBus.dispatch(Events.ENTER_CREDENTIALS);
		} else {
			// populate envelope account if credentials is valid
			User user = new User();
			user.setPassword(envelopePreferences.password().getValue());
			user.setUsername(envelopePreferences.username().getValue());
			EnvelopeProvider.instance().getEnvelope().setUser(user);
			eventBus.dispatch(Events.LOAD_USER);
		}
	}

	/**
	 * Display content view to user with empty message.
	 */
	public void showContent(final Widget contentWidget) {
		view.setContent(contentWidget);
	}

	/**
	 * Display content view to user with given message.
	 * 
	 * @param contentWidget
	 *            the content to display
	 * @param message
	 *            the message to show
	 */
	public void showContent(final Widget contentWidget, final String message) {
		showMessage(message);
		view.setContent(contentWidget);
	}

	public void showMessage(final String message) {
		if (!"".equals(message)) {
			view.showMessage(new Label(message));
		}
	}

	public void setInfo(final String info) {
		view.setInfo(new Label(info));
	}

	public void hideMessage() {
		view.hideMessage();
	}

	public void updateEnvelopeSize(final EnvelopeSize envelopeSize) {
		view.updateEnvelopeSize(envelopeSize);
	}

	private boolean isCredentialsValid() {
		boolean result = false;
		if (null != envelopePreferences) {
			String password = envelopePreferences.password().getValue();
			String username = envelopePreferences.username().getValue();
			result = !"".equals(username) || !"".equals(password);
		}
		return result;
	}
}
